package com.aya.aop.custom.pointcut;

import org.aopalliance.aop.Advice;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.aop.Pointcut;
import org.springframework.aop.PointcutAdvisor;
import org.springframework.aop.aspectj.AspectJExpressionPointcut;
import org.springframework.stereotype.Service;


@Service
public class MyAdvisor implements PointcutAdvisor {
    private static final Logger logger = LoggerFactory.getLogger(MyAdvisor.class);
    @Override
    public Advice getAdvice() {
        return new MethodInterceptor() {
            @Override
            public Object invoke(MethodInvocation invocation) throws Throwable {
                logger.info("before, class:{},method:{}",invocation.getThis().getClass().getName(),invocation.getMethod().getName());
                Object proceed = invocation.proceed();
                logger.info("after, class:{},method:{}",invocation.getThis().getClass().getName(),invocation.getMethod().getName());
                return proceed;
            }
        };
    }

    @Override
    public boolean isPerInstance() {
        return true;
    }

    @Override
    public Pointcut getPointcut() {
        AspectJExpressionPointcut aspectJExpressionPointcut = new AspectJExpressionPointcut();
        aspectJExpressionPointcut.setExpression("execution(* com.aya.aop.custom.pointcut..*(..))");
        return aspectJExpressionPointcut;
    }
}
